<?php


namespace App\Job;


use App\Config\CacheConfig;
use App\Http\Library\Lock;
use App\Http\Library\Pfs;
use App\Http\Library\Queue;
use App\Http\Model\Job\PfsHeadAddbatchtransferModel;
use App\Http\ModelDao\Job\PfsHeadAddbatchtransferDao;
use App\Http\ServiceBase\Job\PfsHeadAddbatchtransferBase;

class PfsHeadAddbatchtransferJob
{

    /**
     * 招行-提出划拨/解冻   机构端 -> 招行
     *
     * @param array $data
     */
    public static function main(array $data)
    {
        try {
            //return PfsHeadAddbatchtransferModel::save($data, false,'合肥培优资金划拨任务暂停');
            /*查询当日在本次划拨记录前是否已存在划拨记录*/
            $last = PfsHeadAddbatchtransferModel::last($data);
            if (empty($data['transfer_id']) && !empty($last)) {
                /*拆分可以发起划拨、划拨失败||划拨中的记录*/
                $group = PfsHeadAddbatchtransferBase::group($data, $last);
                /*重置任务内容*/
                if (!empty($group['new'])) {
                    $data = $group['new'];
                    /*重置$last*/
                    $last = PfsHeadAddbatchtransferModel::last($data);
                    /*单独执行划拨中的的任务*/
                    if (!empty($group['exist'])) {
                        Queue::clock($group['exist'], 10);
                    }
                }
            }
            /*串行锁-key*/
            $lock = CacheConfig::PFS_HEAD_ADDBATCHTRANSFER . ':' . $data['pfs_dw_id'];
            /*串行化处理*/
            if (Lock::lock($lock) === false) {
                Queue::clock($data, 10);
                throw new \Exception('没有拿到串行锁');
            }
            /*本次申请划拨总金额*/
            $sumTransferAmt = 0;
            $createDate = null;
            $transferDate = null;
            /*获取需要同步的transferList结构*/
            $transferList = PfsHeadAddbatchtransferDao::transferList($data, $sumTransferAmt, $createDate, $last, $transferDate);
            $result = [];
            /*划拨申请编号，系统内唯一*/
            if (empty($data['transfer_id'])) {
                /*这里一定要串行化调用,招行接口限制长度是22,跟文档中给出的32不一致,22并行调用可能会造成transfer_id重复*/
                $data['transfer_id'] = PfsHeadAddbatchtransferDao::transferId();
            }
            /*划拨申请课程数*/
            $data['transfer_num'] = count($transferList);
            /*本次申请划拨总金额*/
            $data['sum_transfer_amt'] = $sumTransferAmt;
            /*如果存在划拨处理中的记录,则等待划拨完成后再调用下次划拨*/
            if (PfsHeadAddbatchtransferBase::transferIng($last) === true) {
                Queue::clock($data, 1860);
                throw new \Exception('存在多笔划拨记录,需要等待记录完成,则可以发起本次划拨');
            }
            /*提出划拨/解冻   机构端 -> 招行*/
            $result = Pfs::pfsHeadAddBatchTransfer([
                /*机构编号，由招行分配*/
                'agencyId'       => $data['pfs_agencyid'],
                /*划拨申请编号，系统内唯一*/
                'transferId'     => $data['transfer_id'],
                /*划拨申请课程数*/
                'transferNum'    => $data['transfer_num'],
                /*本次申请划拨总金额*/
                'sumTransferAmt' => $sumTransferAmt,
                /*申请日期，格式：YYYY-MM-DD*/
                'applyDate'      => date('Y-m-d'),
                /*销课的时间，一般传T-1日期。（如果是销课模式，则必传。）*/
                'date'           => $createDate,
                /*划拨申请列表*/
                'transferList'   => $transferList,
            ], [
                /*招行-加密密钥*/
                'pfs_encrypt_private' => $data['pfs_encrypt_private'],
                /*招行-签名密钥*/
                'pfs_sign_private'    => $data['pfs_sign_private']
            ]);
            /*招行-提出划拨/解冻>机构端>招行-入库*/
            PfsHeadAddbatchtransferModel::save($data, $result);
        } catch (\Exception $exception) {
            log_arr([
                '[提出划拨&解冻-异常]',
                'line'   => $exception->getLine(),
                'file'   => $exception->getFile(),
                'msg'    => $exception->getMessage(),
                'data'   => serialize($data),
                'last'   => $last,
                'result' => $result,
            ], 'pfs');
            /*招行-提出划拨/解冻>机构端>招行-入库*/
            PfsHeadAddbatchtransferModel::save($data, $result, $exception->getMessage());
            throw new \Exception($exception->getMessage());
        }
        /*释放锁*/
        Lock::unlock($lock);
    }

}





/**
 * PfsHeadAddbatchtransferJob.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/7 18:15:00   操作:创建
 **/
